import { Controller, Get, Param, Query } from '@nestjs/common';
import { ArticleService } from './article.service';
import { ArticleResponseDto } from '../../dto/article.dto';
import { Public } from '../../common/decorators/public.decorator';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';

@ApiTags('文章管理')
@Controller('api/articles')
export class ArticlePublicController {
  constructor(private readonly articleService: ArticleService) {}

  @Public()
  @Get()
  @ApiOperation({
    summary: '获取文章列表',
    description: '获取文章列表，支持类型、分类、搜索词筛选和分页',
  })
  @ApiQuery({
    name: 'categoryId',
    description: '分类ID',
    required: false,
  })
  @ApiQuery({
    name: 'search',
    description: '搜索关键词',
    required: false,
  })
  @ApiQuery({
    name: 'page',
    description: '页码',
    required: false,
    default: 1,
  })
  @ApiQuery({
    name: 'limit',
    description: '每页数量',
    required: false,
    default: 10,
  })
  @ApiQuery({
    name: 'isActive',
    description: '文章状态：-1待审核，0审核失败，1审核成功（默认只返回审核成功的文章）',
    required: false,
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
  })
  findAll(
    @Query('categoryId') categoryId?: string,
    @Query('search') search?: string,
    @Query('page') page: number = 1,
    @Query('limit') limit: number = 10,
    @Query('isActive') isActive?: string,
  ) {
    return this.articleService.findAll({
      categoryId: categoryId ? +categoryId : undefined,
      search,
      page,
      limit,
      isActive: isActive ? +isActive : undefined,
    });
  }

  
  @Public()
  @Get('popular')
  @ApiOperation({
    summary: '获取热门文章列表',
    description: '获取按点赞数排序的热门文章列表',
  })
  @ApiQuery({
    name: 'limit',
    description: '返回的文章数量限制',
    required: false,
    default: 10,
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: [ArticleResponseDto],
  })
  getPopularArticles(@Query('limit') limit?: string): Promise<ArticleResponseDto[]> {
    return this.articleService.getPopularArticles(parseInt(limit || '10') || 10);
  }



  
  @Public()
  @Get(':id')
  @ApiOperation({
    summary: '获取文章详情（公开）',
    description: '获取指定文章的详细信息，只返回已发布的文章',
  })
  @ApiParam({
    name: 'id',
    description: '文章ID',
    example: '1',
  })
  @ApiResponse({
    status: 200,
    description: '获取成功',
    type: ArticleResponseDto,
  })
  @ApiResponse({
    status: 404,
    description: '文章不存在',
  })
  findOnePublic(@Param('id') id: string): Promise<ArticleResponseDto> {
    return this.articleService.findOne(+id);
  }

}